import { llog, lerror } from './logger';
import fetch from 'node-fetch';

export async function pullPriceFake() {
	let price = (Math.random() * 30 + 25).toFixed(1);
	let time = new Date();
	return { price, time };
}

export async function pullPriceOKCoin() {
	try {
		let url = 'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd';
		let res = await fetch(url);
		let text = await res.text();
		let body = JSON.parse(text);
		return {
			price: parseFloat(body.ticker.last),
			time: body.date * 1000, // .toLocaleString(),
		};
	} catch (e) {
		lerror('pullPriceOKCoin fail', e);
	}
}
